package com.weishenai.dao.impl;

import com.weishenai.Builder.BaseRule;
import com.weishenai.Entity.UserInfo;
import com.weishenai.Mapper.QueryMapper;
import com.weishenai.Mapper.UpdateMapper;
import com.weishenai.Utils.IdUtils;
import com.weishenai.dao.UserInfoDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: weishenai
 * @Date: 2021/10/07/11:32
 * @Description:
 */
public class UserInfoDaoImpl implements UserInfoDao {

    @Override
    public List<UserInfo> getUserInfoList(String search, int pageSize, int pageNum) {
        BaseRule baseRule = BaseRule.getInstance();
        baseRule.andLike("name", search);
        QueryMapper queryMapper = new QueryMapper();
        List<UserInfo> userInfos = new ArrayList<>();
        List<Map<String, Object>> userInfo = queryMapper.selectByPage(baseRule, "user_info", pageNum, pageSize);
        if (userInfo.size() != 0) {
            for (Map<String, Object> map : userInfo) {
                UserInfo userInfo1 = new UserInfo((String) map.get("id"),
                        (String) map.get("name"),
                        (int) map.get("age"),
                        (String) map.get("sex"),
                        (String) map.get("address"),
                        (String) map.get("sickId"));
                userInfos.add(userInfo1);
            }
        }
        return userInfos;
    }

    @Override
    public int getUserInfoCount() {
        QueryMapper queryMapper = new QueryMapper();
        return queryMapper.getCount("user_Info");
    }

    @Override
    public int insertUserInfo(UserInfo userInfo) {
        userInfo.setId(IdUtils.getId());
        return new UpdateMapper().insert(userInfo);
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {
        return new UpdateMapper().update(userInfo);
    }

    @Override
    public int deleteById(String id) {
        return new UpdateMapper().deleteById("user_info",id);
    }
}
